﻿using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour 
{
	public float turnSmoothing = 15f;
	public float speedDampTime = 0.1f;
	public float speed;
	public float jumpSpeed;
	public float turnSpeed;
	public float runSpeed;
    public float jumpAnimationSpeed;
	public float jumpCooldown;


	public AnimationClip idle;
	public AnimationClip walk;
	public AnimationClip jump;
	public AnimationClip run;
	public AnimationClip shooting;

	
    private new Animation animation;
	private Animation anim;
	private Transform playerTransform;
	private CharacterController controller;
	private bool isRunning;
	private float verticalVelocity;
	private Vector3 moveDirection;
	private Vector3 impact;
	private bool ableJump = true;
	private float jumpTimer;

	private float remainingHeight;
	
	void Awake()
	{
		playerTransform = transform;
		animation = GetComponent<Animation> ();
		controller = GetComponent<CharacterController> ();
		//animation["Shooting"].AddMixingTransform(playerTransform.Find("Bip02/Bip02 Pelvis/Bip02 Spine/Bip02 Spine1/Bip02 Neck/Bip02 L Clavicle"));
		animation["Shooting"].AddMixingTransform(playerTransform.Find("Bip02/Bip02 Pelvis/Bip02 Spine"));
		verticalVelocity = 0.0f;
		moveDirection = Vector3.zero;
		impact = Vector3.zero;
		jumpTimer = jumpCooldown;
	}
	
	void FixedUpdate()
	{
		float h = Input.GetAxis("Horizontal");
		float v = Input.GetAxis("Vertical2");
		if (impact.magnitude > 0.2) 
		{
			controller.Move(impact * Time.deltaTime);
		}
		impact = Vector3.Lerp(impact, Vector3.zero, 5*Time.deltaTime);
		MovementManagement (h, v);
	}
	
	void MovementManagement(float horizontal, float vertical)
	{
		transform.Rotate(0, horizontal * turnSpeed * Time.deltaTime, 0);
        
		if(horizontal != 0f || vertical != 0f)
		{
			if(Input.GetKey(KeyCode.LeftShift) && !Input.GetKey(KeyCode.LeftControl))
			{
                if (Input.GetKey(KeyCode.W))
                {
                    moveDirection = playerTransform.TransformDirection(Vector3.forward) * speed;
                    isRunning = false;
                }
                else if (Input.GetKey(KeyCode.S))
                {
                    moveDirection = playerTransform.TransformDirection(Vector3.back) * speed;
                    isRunning = false;
                }
			}
			else
			{
                if (Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.LeftControl))
                {
                    moveDirection = playerTransform.TransformDirection(Vector3.forward) * runSpeed;
                    isRunning = true;
                }
                else if (Input.GetKey(KeyCode.S))
                {
                    moveDirection = playerTransform.TransformDirection(Vector3.back) * speed;
                    isRunning = false;
                }
                
			}

			if(!animation.IsPlaying(jump.name))
			{
				if(isRunning)
				{
					animation.Play( run.name );
				}
				else
				{
					animation.Play( walk.name );
				}
			}
		}
		else
		{
			moveDirection = playerTransform.TransformDirection(Vector3.forward) * 0.000000000001f;
			if(!animation.IsPlaying(jump.name))
				animation.Play( idle.name );
		}

		if(controller.isGrounded)
		{
            animation[jump.name].speed = 1;
            verticalVelocity = 0.0f;
		}

		if(Input.GetKey(KeyCode.Space) && ableJump && !Input.GetKey(KeyCode.LeftControl))
		{
			if(controller.isGrounded)
			{
                animation.Play( jump.name );
                animation[jump.name].speed = jumpAnimationSpeed;
				remainingHeight = jumpSpeed;
				jumpTimer = 0;
			}
		}
		verticalVelocity -= 19.6f * Time.deltaTime;
		moveDirection.y = remainingHeight + verticalVelocity;

		if(remainingHeight > 0)
		{
			remainingHeight -= 1;
		}

		if(jumpTimer == 0)
		{
			ableJump = false;

		}

		if(!ableJump)
		{
			jumpTimer++;
			if(jumpTimer>jumpCooldown)
			{
				ableJump=true;
			}
		}

		controller.Move (moveDirection * Time.deltaTime);
	}

	public void AddImpact(Vector3 dir, float force)
	{
		dir.Normalize();
		if (dir.y < 0)
		{
			dir.y = -dir.y; 
		}// reflect down force on the ground
		impact += dir.normalized * force ;
	}

}
